home *** CD-ROM | disk | FTP | other *** search
/ Directorty Opus 5 - Magellan 2 / Opus 5 - Magellan 2.iso / Extras / D51_NUSource / Source / Routines / System.s < prev   
Text File  |  1996-01-06  |  15KB  |  395 lines

  1. *=- INFO -=****************************************************************************
  2. * System Subroutines. (File access, Communication, etc)                      *
  3. * Unless otherwise stated, all routines are by Leo 'Nudel' Davidson              *
  4. *=- Notes -===========================================================================*
  5. * o Error should always be called using the Beq_ErrorX macros in Constants.i          *
  6. *=- To Do -===========================================================================*
  7. * o DeleteNudelPort Should ensure that all msgs have been replied before deletion.    *
  8. *   (Currently, all programs using *NudelPort should never get non-reply messages).   *
  9. * o Error should put up an alert when any of its sub-routines fail.              *
  10. *=- History -=========================================================================*
  11. * o Version 2.00                                      *
  12. *   Ancient history deleted.                                  *
  13. ***************************************************************************************
  14.  
  15.  
  16. ***************************************************************************************
  17. * System Subroutines                                      *
  18. ***************************************************************************************
  19. ;;; "Error" is not optional.
  20. ;SYS_READARG    ; ReadArgs() routine.
  21. ;SYS_FREEARG    ; FreeArgs() routine.
  22. ;SYS_CLIRITE    ; Write text using CLI_Hdl(a5) as output handle.
  23. ;SYS_CHKCRTC    ; Check for ^C-Break and abort program if sent.
  24. ;SYS_PORTS    ; CreateMsgPort(), DeleteMsgPort() routines.
  25.  
  26. ;Include Asm:Source/Routines/System.s
  27.  
  28. ;-------------------------------------------------------------------------------------;
  29. ;- STD_F_1 ---------------------------------------------------------- Standard Flags -;
  30. ;-------------------------------------------------------------------------------------;
  31. SF1_ReadArgs    Equ    0    RDArgs Structure requires FreeArgs()
  32. SF1_OpnLibErr    Equ    1    If set, one or more libs couldn't be openned.
  33. SF1_ErrorReq    Equ    2    If set, next error routine output w/ a req.
  34. ;                (Reset to default after every error call).
  35. ;                NOTE: This _may_ be used by other functions.
  36.     IFD    DOpus5_Error    Make SURE it causes an error if used wrongly.
  37. SF1_ErrorDop    Equ    3    If set, next error will output via "dopus request"
  38.     ENDC
  39. SF1_OpenIFF    Equ    4    If set, NudelIFFHandle(a5) requires CLOSE.
  40. SF1_NewListers    Equ    5    Get(Source|Dest)Handle allowed to create new listers?
  41.     IFD    SYS_PORTS
  42. *= Create NudelMsgPort (NORMAL) =**************************************= 11-Aug-1995 =*
  43. *  Inputs: None.                                      *
  44. * Outputs: NudelPort(a5) - Ptr to our MsgPort                          *
  45. ***************************************************************************************
  46. CreateNudelPort
  47.     N_CallExec    CreateMsgPort            -.
  48.     Move.l    d0,NudelPort(a5)             |- Create our msgport.
  49.     Beq_ErrorE    ErrAct_CreateMsgPort(pc),#0    -'
  50.     RTS
  51.  
  52. ErrAct_CreateMsgPort
  53.     Dc.b    "Could not create MsgPort",0
  54.     Even
  55.  
  56. *= Delete NudelMsgPort (NORMAL) =**************************************= 11-Aug-1995 =*
  57. *  Inputs: NudelPort(a5) - Ptr to our MsgPort                          *
  58. * Outputs: NudelPort(a5) - Null.                              *
  59. *=====================================================================================*
  60. *   Notes: Ensures that all msgs have been GetMsg()'d before DeleteMsgPort.           *
  61. ***************************************************************************************
  62. DeleteNudelPort
  63.     Tst.l    NudelPort(a5)        -._ If no MsgPort to free,
  64.     Beq.s    DMsgSkp            -'  skip this routine.
  65.     N_CallExec    Forbid        Forbid Multitasking so no new messages sent.
  66.  
  67. DNP_WP_Loop
  68.     Move.l    NudelPort(a5),a0    -._ Get first waiting
  69.     N_CallExec    GetMsg        -'  message sent to us.
  70.     Tst.l    d0            -._ If there are none left,
  71.     Beq.s    DNP_WP_Skip        -'  It's safe to close down.
  72.     Bsr_ErrorW    ErrAct_PendingMessages(pc),#0
  73.     Bra.s    DNP_WP_Loop
  74. DNP_WP_Skip
  75.  
  76.     Move.l    NudelPort(a5),a0    -._ Delete
  77.     N_CallExec    DeleteMsgPort    -'  the port.
  78.     Move.l    #0,NudelPort(a5)    Null Pointer to stop re-free.
  79.     N_CallExec    Permit        Permit Multitasking again.
  80. DMsgSkp
  81.     RTS
  82.  
  83. ErrAct_PendingMessages
  84.     Dc.b    "Messages pending at port closure - please report to Leo",0
  85.     Even
  86.     ENDC
  87.  
  88.  
  89.     IFD    SYS_READARG
  90. *= READ ARGS (NORMAL) =**************************************= 24-Jul-1995 =*
  91. * RDA_Template(pc) = Template         * RDA_Array(a5) = Array of LongWords  *
  92. * RDA_Rtn(a5) = (Pointer to returned RDArgs structure)                      *
  93. *===========================================================================*
  94. * RDA_Array(a5) should be initialized to default values before calling.     *
  95. *****************************************************************************
  96. RArgNor    BTst    #SF1_ReadArgs,STD_F_1(a5)    -._ Generate an Error if
  97.     Bne    Internal            -'  previous Read & no Free.
  98.     Lea    RDA_Template(pc),a0    Template for ReadArgs()
  99.     Move.l    a0,d1
  100.     Lea    RDA_Array(a5),a0    Array space for ReadArgs()
  101.     Move.l    a0,d2
  102.     Moveq    #0,d3            Use default options for ReadArgs()
  103.     N_CallDOS    ReadArgs
  104.     Move.l    d0,RDA_Rtn(a5)        Store returned pointer to structure.
  105.     Beq_ErrorE    ErrAct_ReadArgs(pc),#0    Fatal Error, No Filename.
  106.     BSet    #SF1_ReadArgs,STD_F_1(a5)    ReadArgs done, need freeing.
  107. FArgNDn    RTS
  108. ErrAct_ReadArgs
  109.     Dc.b    "Could not parse command-line",0
  110.     Even
  111.     ENDC
  112.  
  113.     IFD    SYS_FREEARG
  114. *= FREE ARGS (NORMAL) =**************************************= 24-Jul-1995 =*
  115. * RDA_Rtn(a5) = Pointer to RDArgs structure returned by ReadArgs()          *
  116. *****************************************************************************
  117. FArgNor    BClr    #SF1_ReadArgs,STD_F_1(a5)
  118.     Beq.s    FArgNDn            If no FreeArgs() needed, skip.
  119.     Move.l    RDA_Rtn(a5),d1
  120.     N_JumpDOS    FreeArgs
  121. ;;;;;;;    RTS for us.
  122.     ENDC
  123.  
  124.  
  125.     IFD    SYS_CLIRITE
  126. *= CLI WRITE =***************************************************************
  127. * a0 = Start of text to be written    * d3 = #Chars to be Written           *
  128. * CLI_Hdl(a5) - Handle of CLI         *                                     *
  129. *****************************************************************************
  130. CLIRite    Move.l    a0,d2
  131. CLIRit2    Move.l    CLI_Hdl(a5),d1
  132.     Lea    CLIRite_NIB_FileName(pc),a0    -._ Set pseudo-filename
  133.     Move.l    a0,NIB_NameAdrs+FakeNIB(a5)    -'  in FakeNIB.
  134.     N_CallDOS    Write            RTS for us.
  135.     Cmpi.l    #-1,d0
  136.     Beq_ErrorE    ErrAct_Write(pc),FakeNIB_Adrs(a5)    Fatal error.
  137.     RTS
  138.  
  139. CLIRite_NIB_FileName
  140.     Dc.b    "standard output"
  141.     EVEN
  142.     ENDC
  143.  
  144.  
  145.     IFD    SYS_CHKCRTC
  146. *= CHECK CTRL-C =******************************************************= 16-Aug-1995 =*
  147. * Checks for if ^C has been pressed, and if so prints "***Break" and Exits          *
  148. * All registers preserved. (But not CCR)                          *
  149. ***************************************************************************************
  150. CkCtrlC    Movem.l    d0-d7/a0-a6,-(SP)    Preserve everything.
  151.     Moveq    #0,d0            No new signals.
  152.     Move.l    #SIGBREAKF_CTRL_C,d1    Clear ^C Signal
  153.     N_CallExec    SetSignal    Do it & OldSignals -> d0
  154.     BTst    #SIGBREAKB_CTRL_C,d0    Was ^C Set?
  155.     Bne.s    YoBreak            YES=Exit
  156.     Movem.l    (SP)+,d0-d7/a0-a6    Restore everything.
  157.     RTS
  158. YoBreak    Lea    BreakMs(pc),a0        -.
  159.     Move.l    #BreakML,d3         |- Write "***Break"
  160.     Bsr.s    CLIRite            -'
  161.     Bra    Finish            and then Exit the Proggy.
  162. BreakMs    Dc.b    10,155,"0m***Break: "
  163.     PROGNAM
  164.     Dc.b    10
  165. BreakML    Equ    *-BreakMs
  166.     Even
  167.     ENDC
  168.  
  169.  
  170. *= ERROR =*************************************************************= 16-Aug-1995 =*
  171. * ErrorE for FATAL errors, and ErrorW for non-fatal (will return to caller).          *
  172. * This routine should ALWAYS be called using the Beq_ErrorX macros in Constants.i     *
  173. *=====================================================================================*
  174. *  Inputs: Buffer1(a5) - Buffer to write message into (#Buffer1Len bytes long)          *
  175. *          Active_NIB(a5)/NIB_NameAdrs - Pointer to name of file (etc.)              *
  176. *          CAction(a5) - Pointer to null-term "Could not lock file" type message.     *
  177. * Outputs: DosRtnC(a5) - #ERROR, #WARN, or left-alone (NOTICE).                  *
  178. *=====================================================================================*
  179. * If Active_NIB(a5) or NIB_NameAdrs are Null, filename is not output.              *
  180. * If CAction(a5) is null, no action is output (will look stupid)              *
  181. * If IoErr() returns zero, no reason output.                          *
  182. ***************************************************************************************
  183. ErrorE_Internal
  184.     Moveq    #0,d1            SetIoErr(#0)
  185.     N_CallDOS    SetIoErr    No Error.
  186. ErrorE    Cmpi.l    #RETURN_ERROR,DosRtnC(a5)    -.
  187.     Bge.s    ErrorESkp             |- "ERROR" return code, unless a worse
  188.     Move.l    #RETURN_ERROR,DosRtnC(a5)    -'  one has already been set.
  189. ErrorESkp
  190.     Lea    ErrHead_Error(pc),a0    Fatal-Error message.
  191.     Bsr.s    Error_Main_Subroutine    As Error-Warn.
  192.     Bra    Finish            Finish the program.
  193. ;-------------------------------------------------------------------------------------;
  194. ErrorN    Moveq    #0,d1            SetIoErr(#0)
  195.     N_CallDOS    SetIoErr    No Error.
  196.     Lea    ErrHead_Notice(pc),a0    "Note" message.
  197.     Bra.s    Error_Main_Subroutine
  198. ;;;;;;;    RTS for us.
  199. ;-------------------------------------------------------------------------------------;
  200. ErrorW    Cmpi.l    #RETURN_WARN,DosRtnC(a5)    -.
  201.     Bge.s    ErrorWSkp             |- "WARN" return code, unless a worse
  202.     Move.l    #RETURN_WARN,DosRtnC(a5)    -'  one has already been set.
  203. ErrorWSkp
  204.     Lea    ErrHead_Warn(pc),a0    Warning-Error message.
  205. ;;;;;;;    Bra.s    Error_Main_Subroutine
  206. ;;;;;;;    RTS for us.
  207. ;-------------------------------------------------------------------------------------;
  208. Error_Main_Subroutine
  209.     Move.l    a0,RDF_1_Long(a5)    "Error"/"Warn"/"Notice" header is first %s
  210.     Move.l    CAction(a5),RDF_2_Long(a5)    Action is the second %s
  211.     Tst.l    Active_NIB(a5)        -.
  212.     Bne.s    EW_GotNIB_Skp         |- If no active NIB, no filename.
  213.     Move.l    #0,RDF_3_Long(a5)    -'
  214.     Bra.s    EW_NoNIB_Skp
  215. EW_GotNIB_Skp
  216.     Move.l    Active_NIB(a5),a0        -._ Adrs of filename from NIB.
  217.     Move.l    NIB_NameAdrs(a0),RDF_3_Long(a5)    -'  If null, no string output.
  218. EW_NoNIB_Skp
  219.     Move.l    #0,Active_NIB(a5)    No active NIB.
  220.  
  221.     Moveq    #0,d1            SetIoErr(#0) so no error in the future.
  222.     N_CallDOS    SetIoErr    Get number representing the error.
  223.     Move.l    d0,d1            Error code to d1 for Fault()
  224.  
  225.     Lea    ErrReason_Header(pc),a0    -._ Header to d2
  226.     Move.l    a0,d2            -'  for Failt()
  227.     Lea    Buffer1(a5),a0        -._ Buffer to build message in.
  228.     Move.l    a0,d3            -'  To d3 for Fault()
  229.     SF    (a0)            Ensure it's null-terminated to start with.
  230. ;;;;;;;            So that it can be used even if Fault() doesn't write into it.
  231.     Move.l    #Buffer1Len,d4        Size of the buffer to d4 for Fault()
  232.     N_CallDOS    Fault        Put error message into the buffer.
  233. ;;;;;;;    Tst.l    d0            Ignore result, Buffer1 can be used anyway.
  234.  
  235.     Lea    Buffer1(a5),a0        -._ Adrs of reason is the
  236.     Move.l    a0,RDF_4_Long(a5)    -'  fourth %s for RawDoFmt()
  237. ;-------------------------------------------------------------------------------------;
  238.     Lea    RDF_Error_Input(pc),a0        Input string.
  239.     Lea    RDF_Array(a5),a1        DataArray
  240.     Lea    Buffer2(a5),a3            -._ Output
  241.     Move.l    a3,RDF_Adrs(a5)            -'  Buffer
  242.     Move.l    #Buffer2Len,RDF_Size(a5)    Size of output buffer.
  243.     Bsr    NudelRawDoFmt            Create the full error message.
  244. ;-------------------------------------------------------------------------------------;
  245. ; Error string is built, not output it in whatever way...                  ;
  246. ;-------------------------------------------------------------------------------------;
  247.     IFD    DOpus5_Error        If DOpus5 error messages are allowed, check.
  248.     BClr    #SF1_ErrorDop,STD_F_1(a5)
  249.     Bne.s    ErrDopus5
  250.     ENDC
  251.  
  252.     IFND    No_Requesters        If No_Requesters, go straight to Shell.
  253. ;;;;;;;                    (Excludes the ErrReq routine as well)
  254.     IFD    Reqs_S
  255.     Tst.l    Reqs_S(a5)        The Reqs/S command defaults to shell.
  256.     Beq.s    ErrShell
  257.     ENDC
  258.  
  259.     IFD    NoReq_S            The NoReq/S command defauls to requester.
  260.     Tst.l    NoReq_S(a5)
  261.     Bne.s    ErrShell
  262.     ENDC
  263.  
  264.     IFND    NoReq_S
  265.     IFND    Reqs_S
  266.     SetReqDefault            If Reqs / NoReqS not part of RDA_Template.
  267.     Beq.s    ErrShell
  268.     ENDC
  269.     ENDC
  270. ;-------------------------------------------------------------------------------------;
  271. ; Requester error output.                                  ;
  272. ;-------------------------------------------------------------------------------------;
  273. ErrReq
  274.     Move.l    #N_EasyStruct_Len,N_ES_Length(a5)    Size of structure.
  275.     Move.l    #0,N_ES_Flags(a5)            No flags.
  276.     Lea    ErrReq_Title(pc),a0        -.
  277.     Move.l    a0,N_ES_Title(a5)         |
  278.     Lea    Buffer2(a5),a0             |_ Texts for
  279.     Move.l    a0,N_ES_Body(a5)         |  requester.
  280.     Lea    ErrReq_Gads(pc),a0         |
  281.     Move.l    a0,N_ES_Gadgets(a5)        -'
  282.  
  283.  
  284.     Lea    N_EasyStruct(a5),a1    Point to EasyRequest structure.
  285.     Sub.l    a0,a0            Specify default public screen.
  286.     Move.l    a0,a2            No IDCMP flags.
  287.     Move.l    a0,a3            No arguments.
  288.     N_JumpInt    EasyRequestArgs    Put up the req. NO ERROR CHECKING, on purpose.
  289. ;;;;;;;    RTS for us.
  290.  
  291.     ENDC
  292. ;-------------------------------------------------------------------------------------;
  293. ; Shell error output.                                      ;
  294. ;-------------------------------------------------------------------------------------;
  295. ErrShell
  296.     Lea    ErrReq_Title(pc),a0    -.
  297.     Move.l    a0,d2             |_ Write "ProgName: "
  298.     Move.l    #ErrReq_Title_Len,d3     |  for start of msg.
  299.     Bsr.s    ErrShell_Write        -'
  300.  
  301. ; Must work out the length of the returned Error text.
  302.  
  303.     Lea    Buffer2(a5),a0
  304.     Move.l    a0,d2
  305.     Bsr    NullLen
  306.     Move.l    d0,d3
  307.  
  308. ; A return must be added. As the string is null-terminated, and we don't need
  309. ; the null, the return can be written over the null without any buffer length
  310. ; checking.
  311.  
  312.     Move.l    d2,a0            -.
  313.     Add.l    d3,a0             |- Point to null and replace with return.
  314.     Move.b    #10,(a0)        -'
  315.     Addq.l    #1,d3            Update length of text to output.
  316. ;;;;;;;    Bra.s    ErrShell_Write
  317. ;;;;;;;    Bra.s for us.
  318. ;;;;;;;    RTS for us.
  319.  
  320. ErrShell_Write
  321.     Move.l    CLI_Hdl(a5),d1
  322.     N_CallDOS    Write        NO ERROR CHECKING, on purpose.
  323.     RTS
  324. ;-------------------------------------------------------------------------------------;
  325. ; Directory Opus 5 error output.                              ;
  326. ;-------------------------------------------------------------------------------------;
  327.     IFD    DOpus5_Error
  328. ErrDopus5
  329.     Lea    Buffer2(a5),a0            -._ Error message is the
  330.     Move.l    a0,RDF_1_Long(a5)        -'  first (and only) %s
  331.  
  332.     Lea    Err_Dopus_Request_Command(pc),a0    Input string.
  333.     Lea    RDF_Array(a5),a1        DataArray
  334.     Lea    Buffer1(a5),a3            -._ Output
  335.     Move.l    a3,RDF_Adrs(a5)            -'  Buffer
  336.     Move.l    #Buffer1Len,RDF_Size(a5)    Size of output buffer.
  337.     Bsr    NudelRawDoFmt            Create the full error message.
  338.  
  339.     Lea    Dopus_Front_Command(pc),a1    -._ Bring the DOpus
  340.     Bsr    FillAndSendNudelRexxMsg        -'   screen to the front.
  341.  
  342.     Lea    Buffer1(a5),a1            -._ Put up the
  343.     Bra    FillAndSendNudelRexxMsg        -'  requester.
  344. ;;;;;;;    RTS for us.
  345.  
  346. Dopus_Front_Command
  347.     Dc.b    "dopus front",0
  348. Err_Dopus_Request_Command
  349.     Dc.b    'dopus request "%s" _OK',0
  350.     ENDC
  351. ;-------------------------------------------------------------------------------------;
  352. ; Strings.                                          ;
  353. ;-------------------------------------------------------------------------------------;
  354. ErrReq_Title
  355.     PROGNAM
  356.     Dc.b    ": "
  357. ErrReq_Title_Len Equ *-ErrReq_Title
  358.  
  359.     IFND    No_Requesters
  360.     Dc.b    0            ;Null-term for ErrReq_Title
  361.  
  362. ErrReq_Gads
  363.     Dc.b    "OK",0
  364.     ENDC
  365.  
  366. RDF_Error_Input
  367.     Dc.b    "%s - %s%s%s",0
  368.  
  369. ErrHead_Error
  370.     Dc.b    "Error",0
  371. ErrHead_Warn
  372.     Dc.b    "Warning",0
  373. ErrHead_Notice
  374.     Dc.b    "Note",0
  375.  
  376. ErrReason_Header
  377.     Dc.b    10,"Reason",0
  378.  
  379. ErrAct_Write
  380.     Dc.b    "Could not write to ",0
  381. ErrAct_AllocMem
  382.     Dc.b    "Could not allocate memory for ",0
  383.     EVEN
  384. ***************************************************************************************
  385. Internal
  386.     Bra_ErrorE_Int    ErrAct_Internal(pc),#0
  387. Buffer_Overflow
  388.     Bra_ErrorE_Int    ErrAct_BufferOverflow(pc),#0
  389.  
  390. ErrAct_Internal
  391.     Dc.b    "Internal error!",0
  392. ErrAct_BufferOverflow
  393.     Dc.b    "buffer overflow",0
  394.     Even
  395.